<?php
/**
 * Contains {@link Domain51_Auth_Adapter_Curl}
 *
 * @package     Domain51
 * @subpackage  Domain51_Auth
 */

/**
 * Required library
 */
Solar::loadClass('Solar_Auth_Adapter');

/**
 * A basic class to handle user authentication via a remote URL.
 *
 * This class uses the cURL extension of PHP to attempt to log in to an external
 * website for verification.
 *
 * This class is considered an abstract.  A user could, however, configure it
 * via their Solar.config.php file and use it without extending it.  Extending
 * it as {@link Domain51_Auth_Adapter_Gmail} does allows the programmer to 
 * specify everything that is required directly in the class definition.
 *
 * @package     Domain51
 * @subpackage  Domain51_Auth
 * @license     Release under the same license as Solar
 */
class Domain51_Auth_Adapter_Curl extends Solar_Auth_Adapter
{
    /**
     * User-provided configuration values
     *
     * Keys are:
     * `url`
     * : (string) URL to make authorization request against
     *
     * `curl`
     * : (array) An array of options to use with 
     *           {@link curl_setopt http://www.php.net/curl_setopt}.
     *
     * @var     array
     * @access  protected
     */
    protected $_Domain51_Auth_Adapter_Curl = array(
        'url' => '',
        'curl_options' => array()
    );
    
    
    /**
     * Handle instantiation.
     *
     * In addition to performing the normal Solar construction, this checks to
     * insure that the cURL extension is loaded.
     *
     * @param   array|null
     * @return  void
     */
    public function __construct($config = null) {
        parent::__construct($config);
        
        // throw an exception if cURL extension is not loaded
        if (!extension_loaded('curl')) {
            throw $this->_exception(
                'ERR_EXTENSION_NOT_LOADED',
                array($this->_config)
            );
        }
    }
    
    
    /**
     * Verifies the {@link $_handle} and {@link $_passwd} against the config 
     * value's url to insure the credentials provided are correct.
     *
     * @return  boolean
     * @access  protected

     */
    protected function _verify() {
        $url = $this->_config['url'];
        
        // throw an exception if $url has not been set by config or a sub-class
        if (empty($url)) {
            throw $this->_exception(
                'Curl Authorization Error: url config value not set',
                array($this->_config)
            );
        }
        
        // Instantiate cURL
        $curl = curl_init($url);
        
        // Set all options as specified in config value
        foreach ($this->_config['curl_options'] as $name => $value) {
            curl_setopt($curl, $name, $value);
        }
        
        // set username/password and execute
        curl_setopt($curl, CURLOPT_USERPWD, "{$this->_handle}:{$this->_passwd}");
        $result = curl_exec($curl);
        if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200) {
            return true;
        }
        
        // attempt parent::_verify() as this has failed 
        return parent::_verify();
    }

}

?>
